<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui"%>
<%@ taglib uri="/WEB-INF/tld/fmt.tld" prefix="fmt"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="renderer" content="webkit">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>项目调息</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
	<div class="Wrapper">
		<div id="pageInTab" class="pageInTab">
			<form id="scm_bsc_info_form">
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="caption">
						<span class="title">项目基础信息</span>
					</div>
					<div class="fieldBox col_50">
						<div class="label">项目名称</div>
						<div class="field">
							<span name="FNCPRJNM" id="FNCPRJNM" >${bscinfo.FNCPRJNM}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">项目编号</div>
						<div class="field">
							<span name="FNCPRJID" id="FNCPRJID" >${bscinfo.FNCPRJID}</span>
							<input type="hidden" id="payschid" name="payschid" value="${payschid}">
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">创建日期</div>
						<div class="field">
							<span name="CRTTM" id="CRTTM" > 
								<fmt:parseDate value="${bscinfo.CRTTM}" var="date"/>
								<fmt:formatDate value="${date}" pattern="yyyy-MM-dd" />
							</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">融资机构</div>
						<div class="field">
							<span name="FUNDRASINSNM" id="FUNDRASINSNM" >${bscinfo.FUNDRASINSNM}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">融资方案</div>
						<div class="field">
							<span name="FNCSCMNM" id="FNCSCMNM" >${bscinfo.FNCSCMNM}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">项目金额</div>
						<div class="field">
							<span name="FNCPRJAMT" id="FNCPRJAMT" >
								${bscinfo.FNCPRJAMT}
							</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">融资金额</div>
						<div class="field">
							<span name="FNCFIAMT" id="FNCFIAMT" >${bscinfo.FNCFIAMT}</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">支付表数量</div>
						<div class="field">
							<span name="PAYCNT" id="PAYCNT" >${bscinfo.PAYCNT}</span>
						</div>
					</div>
				</div>
				<div class="fieldset1 tabel_col tabel_col_s clearFix">
					<div class="caption">
						<span class="title">还款表调息</span>
					</div>
					<div class="fieldBox col_50">
						<div class="label">融资期限</div>
						<div class="field">
							<span name="numberOfPeriods" id="numberOfPeriods" ></span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">调息方式</div>
						<div class="field">
							<span name="intRatTypeNm" id="intRatTypeNm" ></span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">上次调息时间</div>
						<div class="field">
							<span name="preIntTm" id="preIntTm" ></span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">上次基准利率</div>
						<div class="field">
							<span name="preIntRate" id="preIntRate" ></span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">本次调息时间</div>
						<div class="field">
							<span name="thIntTm" id="thIntTm" ></span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">本次基准利率</div>
						<div class="field">
							<span name="thIntRat" id="thIntRat" ></span>
							<input name="regId" id="regId" type="hidden" class="ui-textbox" data-name="regId" value="${regId}" >
						</div>
					</div>
					<div class="caption" style="height:30px;">
						<div class="btnsBox" style="position:relative;">
							<span id="addBtn" class="sq-button sq-button-round" onclick="save();">保存</span>
						</div>
					</div>
					<div class="fieldBox col_100">
						<div id="cost1" class="sq-grid">
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>

	<script type="text/javascript">
	var griddata1;
	var payschId = $("#payschid").val();
	var thIntTm = "";
	var thIntRat = 0;
	var bscRateId = 0;
	var intRatType = "";
	$(function () {
		initDetlGrid();
		initPayReg();
	});
	
    function initDetlGrid() {
    	griddata1 = $("#cost1").ligerGrid({
	    	url: '<%=path%>/interest/management/getDetlGrid.do?payschid='+payschId,
	    	method : 'get',
            columns: [
            { display: '期数', name: 'PAYPD', width: 50, type: 'int' },
            { display: '归集日', width: 100, name: 'GTHRDT',render:renderDate},
            { display: '还款日', width: 100, name: 'PAYDT',render:renderDate},
            { display: '租金', name: 'RENT', width: 150, type: 'int'},
            { display: '本金', name: 'PRINCIPAL', width: 150, type: 'int', editor: { type: 'int' }},
            { display: '利息', name: 'INTEREST', width: 150, type: 'int', editor: { type: 'int' }},
            { display: '其他费用', name: 'OTHEREXPENSES',type: 'int', width: 150, editor: { type: 'int' }},
            { display: '超额抵押', name: 'MORTGAGE',type: 'int', width: 150, editor: { type: 'int' }},
            { display: '剩余本金', name: 'RESIDUALPRINCIPAL',type: 'int', width: 150, editor: { type: 'int' }}
            ],
            usePager:false,
            enabledEdit: true, 
            isScroll: true,
            rownumbers:false, 
            onAfterEdit: f_onAfterEdit,
            onBeforeEdit: f_onBeforeEdit,
            width: '100%'
        });
    }
    
    //计算金额
    function f_onAfterEdit(e) {
    	griddata1.updateCell('RENT', e.record.PRINCIPAL + e.record.INTEREST, e.record); 
    }
    
  	//只允许编辑还款时间大于当前调息时间
    function f_onBeforeEdit(e){ 
  		if($("#thIntTm").html() == ""){
  			 return false;
  		}
        var manager = $("#cost1").ligerGetGridManager();
        var rows = manager.getSelectedRows();
        if (!rows) { alert('请选中该记录后，再编辑!'); return false; }
        var arr = eval(rows);
        for(var i=0;i<arr.length;i++){//定位选中结果集，根据还款时间和当前调息时间是否可以编辑
        	if(e.rowindex == arr[i].__index){
        		rowchecked = true;
        		if(tab(arr[i].PAYDT,$("#thIntTm").html())){
        			return true;
        		}else{
        			return false;
        		}
        	}
         }
        return false;
    }
  
  	//比较日期大小
    function tab(date1,date2){
        var oDate1 = new Date(date1);
        var oDate2 = new Date(date2);
        if(oDate1.getTime() > oDate2.getTime()){
            return true;
        } else {
            return false;
        } 
        return true;
    }
    
  	//初始化数据
	function initPayReg(){
    	if(!payschId)return;
    	var _url = '<%=path%>/interest/management/getById.do';
    	AjaxUtils.ajaxGet(_url, {payschId:payschId}, initCallback);
	}
	
	//初始化回调
	function initCallback(msg){
		if(msg.success == 1){
			 setData(msg.data);//设置数据
		 }else{
			 UIUtils.alertSuccessHold2(msg.info);
			 //UIUtils.alertError(msg.info);
		 }
	}
	
	//修改时候设置页面数据
    function setData(data){
		$("#numberOfPeriods").html(data.numberOfPeriods);
		$("#intRatTypeNm").html(data.intRatTypeNm);
		$("#preIntTm").html(renderDate("","",data.preIntTm));
		$("#preIntRate").html(renderIntRate("","",data.preIntRate));
		$("#thIntTm").html(renderDate("","",data.thIntTm));
		$("#thIntRat").html(renderIntRate("","",data.thIntRat));
		thIntTm = renderDate("","",data.thIntTm);	//本次调息时间
		thIntRat = data.thIntRat;	//本次的调整利率
		intRatType = data.intRatType;		//调息方式
	};

	//保存调息后的数据
	function save(){
		if(thIntTm == ""){
			return;
		}
		$.ligerDialog.confirm('确定更新吗?', function (flag) {
			if(flag == false)return;
			 var regId = $("#regId").val();
			 var data = {"payschId":payschId,"thIntTm":thIntTm,"thIntRat":thIntRat,"intRatType":intRatType,"regId":regId};
			 var jsondata = '{griddata1:'+JSON.stringify(griddata1.getData())+',interest:['+JSON.stringify(data)+']}';
	         $.ajax({
	         	   type: "POST",
	         	   url: "<%=path%>/interest/management/saveInterestInfo.do",
	         	   contentType: 'application/json',
	         	   data: JSON.stringify(jsondata),
	         	   dataType:"json",
	         	   success: function(data){
		         		 if(data.result == '-1'){
		         			top.$.ligerDialog.SQalert({content:"还款表调息失败", SQ_type: "error"});
		         		 }else{
		         			UIUtils.alertSuccessHold2("还款表调息成功");
		         		 }
	         	   }
	         	});
		});
	}
	
	//格式化时间
	function renderDate(rowdata, index, value){
		var dateStr = "";
		if(null != value){
			var date = new Date(value);
			dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
		}
		return dateStr;
	}
	//格式化费率
	function renderIntRate(rowdata, index, value){
		var dateStr = "";
		if(null == value){
			return dateStr;
		}
		return value + "%";
	}
	
	//格式化金额
	function formatMoney(){
		if($("#FNCPRJAMT").html() != ""){
			var fncPrjAmt = $("#FNCPRJAMT").html().toFixed(2);
			if(fncPrjAmt.substr(fncPrjAmt.indexOf(".")+1,fncPrjAmt.indexOf(".")+3) == "00"){
				$("#FNCPRJAMT").html(fncPrjAmt.substr(0,fncPrjAmt.indexOf(".")));
			}else{
				$("#FNCPRJAMT").html(fncPrjAmt);
			}
		}
		if($("#FNCFIAMT").html() != ""){
			var fncFiAmt = $("#FNCFIAMT").html().toFixed(2);
			if(fncFiAmt.substr(fncFiAmt.indexOf(".")+1,fncFiAmt.indexOf(".")+3) == "00"){
				$("#FNCFIAMT").html(fncFiAmt.substr(0,fncFiAmt.indexOf(".")));
			}else{
				$("#FNCFIAMT").html(fncFiAmt);
			}
		}
		
	}
	
	//成功提示-有一个确定按钮 单击后关闭
	UIUtils.alertSuccessHold2 = function(msg){
		 $.ligerDialog.SQalert({content:msg, SQ_showButtons: true,SQ_type: "success",onClose: function () {
			 var selectedTabId = parent.mainApp.parentTab.selectedTabId;
			 var lastTab = parent.parent.tabItems[0];
			 parent.mainApp.parentTab.reload(lastTab.tabid);
			 parent.mainApp.parentTab.removeTabItem(selectedTabId);
			 //parent.parent.reloadAssPackInfoList();
		 }});
	}
	
</script>
</body>
</html>